package org.example.adminservice.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.example.adminservice.OrderService;
import org.example.context.BaseContext;
import org.example.mapper.GoodsMapper;
import org.example.mapper.OrderMapper;
import org.example.pojo.PageBean;
import org.example.pojo.admindto.OrderCheckDTO;
import org.example.pojo.admindto.SaleTableDTO;
import org.example.pojo.adminvo.SaleTableVO;
import org.example.pojo.entity.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private GoodsMapper goodsMapper;

    @Override
    public PageBean getAllOrders(OrderCheckDTO orderCheckDTO) {
        Order order = new Order();
        order.setUsername(
                orderCheckDTO.getUsername()
        );
        order.setGoodname(
                orderCheckDTO.getGoodname()
        );
        order.setState(orderCheckDTO.getState()==null?0:orderCheckDTO.getState());
        Integer page=orderCheckDTO.getPage(),pageSize=orderCheckDTO.getPageSize();
        PageHelper.startPage(page, pageSize);
        List<Order> list = orderMapper.list(order);
        Page<Order> orderPage = (Page<Order>) list;
        PageBean p = new PageBean(orderPage.getTotal(),orderPage.getResult());
        return p;
    }

    public List<SaleTableVO> getSaleTableVO(SaleTableDTO saleTableDTO) {
        List<SaleTableVO> saleTableVO = goodsMapper.saletable(saleTableDTO);
        Double SumPrice=0.0;
        for(SaleTableVO saleTableVO1:saleTableVO){
            saleTableVO1.setSum(saleTableVO1.getPrice()*saleTableVO1.getSalenum());
            SumPrice+=saleTableVO1.getSum();
        }
        SaleTableVO SUM = new SaleTableVO();
        SUM.setName("总计");
        SUM.setSum(SumPrice);
        saleTableVO.add(SUM);
        return saleTableVO;
    }
}
